package cn.broadin.app.hotel.util;

import java.util.Map;
import java.util.Map.Entry;



import cn.broadin.app.hotel.app.AppContext;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * 
 * @author   long
 * @version  1.0.0
 * @2015年5月7日 下午3:02:14
 */
public class XHttpProxy {
	
	public static HttpUtils getHttpUtils(){
		HttpUtils client = new HttpUtils(1000 * 4);
		client.configSoTimeout(1000 * 4);
		client.configResponseTextCharset("UTF-8");
		// 保存服务器端(Session)的Cookie
		PreferencesCookieStore cookieStore = new PreferencesCookieStore(AppContext.getInstance().getBaseContext());
		cookieStore.clear(); 									// 清除原来的cookie
		client.configCookieStore(cookieStore);
		return client;
	}
	
	
	/**
	 * 发送POST请求，获取服务器数据
	 * 
	 * @author long
	 * @create 2015年5月7日 下午3:02:52
	 * @since 
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String sendPostRequest(String url) throws Exception{
		RequestParams requestParams = new RequestParams();
		return sendPostRequest(url, requestParams);
	}

	/**
	 * 发送POST请求，获取服务器数据
	 * 
	 * @author long
	 * @create 2015年5月7日 下午3:02:52
	 * @since 
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String sendPostRequest(String url, Map<String, Object> params) throws Exception{
		XLog.d("[HTTP]-[POST] " + url);
		HttpUtils httpUtils = getHttpUtils();
		try{
			RequestParams requestParams = new RequestParams();
			if (params != null && !params.isEmpty()) {
				for (Entry<String, Object> entry : params.entrySet()) {
					XLog.d(entry.getKey() + " -> " + entry.getValue());
					requestParams.addBodyParameter(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString());
				}
			}
			ResponseStream responseStream = httpUtils.sendSync(HttpRequest.HttpMethod.POST, url, requestParams);
			if(responseStream.getStatusCode() != 200){
				throw new Exception("http server error！！！ statusCode: " + responseStream.getStatusCode());
			}
			return responseStream.readString();
		} catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 发送POST请求，获取服务器数据
	 * 
	 * @author long
	 * @create 2015年5月7日 下午3:02:52
	 * @since 
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String sendPostRequest(String url, RequestParams requestParams) throws Exception{
		XLog.d("[HTTP]-[POST] " + url);
		HttpUtils httpUtils = getHttpUtils();
		try{
			ResponseStream responseStream = httpUtils.sendSync(HttpRequest.HttpMethod.POST, url, requestParams);
			if(responseStream.getStatusCode() != 200){
				throw new Exception("http server error！！！ statusCode: " + responseStream.getStatusCode());
			}
			return responseStream.readString();
		} catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 发送POST请求，获取服务器数据
	 * 
	 * @author long
	 * @create 2015年5月7日 下午3:02:52
	 * @since 
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String sendGetRequest(String url, Map<String, Object> params) throws Exception{
		XLog.d("[HTTP]-[GET] " + url);
		HttpUtils httpUtils = getHttpUtils();
		try{
			RequestParams requestParams = new RequestParams();
			if (params != null && !params.isEmpty()) {
				for (Entry<String, Object> entry : params.entrySet()) {
					XLog.d(entry.getKey() + " -> " + entry.getValue());
					requestParams.addBodyParameter(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString());
				}
			}
			ResponseStream responseStream = httpUtils.sendSync(HttpRequest.HttpMethod.GET, url, requestParams);
			if(responseStream.getStatusCode() != 200){
				throw new Exception("http server error！！！ statusCode: " + responseStream.getStatusCode());
			}
			return responseStream.readString();
		} catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 发送POST请求，获取服务器数据
	 * 
	 * @author long
	 * @create 2015年5月7日 下午3:02:52
	 * @since 
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String sendGetRequest(String url) throws Exception{
		XLog.d("[HTTP]-[GET] " + url);
		HttpUtils httpUtils = getHttpUtils();
		httpUtils.configCurrentHttpCacheExpiry(0);
		try{
			ResponseStream responseStream = httpUtils.sendSync(HttpRequest.HttpMethod.GET, url);
			if(responseStream.getStatusCode() != 200){
				throw new Exception("http server error！！！ statusCode: " + responseStream.getStatusCode());
			}
			return responseStream.readString();
		} catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
}

